var root_path = getRootPath_web();

function getCarLocation() {
    var apiUrl = root_path + "/getLocation.do";
    // console.log(apiUrl);
    $.ajax({
        url: apiUrl,
        type: "GET",
        cache: false,
        dataType: 'json',
        success: function (result) {
            console.log(result);
            var data;
            console.log("currentCarId="+currentLineId);
            for (let i = 0; i < result.data.length; i++) {
                if(result.data[i].lineId === currentLineId) {
                    data = result.data[i];//路线1
                    break;
                 }
            }
            // var xy = [data.lng, data.lat];
            console.log(data);
            // var xy = bd09towgs84(data.lng, data.lat);
            var xy = wgs84tobd09(data.lng, data.lat);

            // var aass = wgs84tobd09(118.146545, 24.443636);
            // console.log(aass);
            console.log("currentCarLocation"+xy);
            // 创建地图实例
            var allOverlay = map.getOverlays();
            for (var i = 0; i < allOverlay.length; i++) {
                if (allOverlay[i].id == "car1") {
                    map.removeOverlay(allOverlay[i]);
                }
            }
            var car_point = new BMap.Point(xy[0], xy[1]);
            var icon = new BMap.Icon(root_path + '/resources/img/yxy_car.png', new BMap.Size(48, 48), {
                anchor: new BMap.Size(10, 30)
            });
            // 创建点坐标
            var marker = new BMap.Marker(car_point, {
                icon: icon
            });
            marker.id = "car1";
            map.addOverlay(marker);


            var index = getClosestSite(xy, sitesArr);
            // var index = getClosestSite(car_point, sitesArr);
           /* if (sitesNames != null && sitesNames.length != 0) {
                console.log("最近站点:" + sitesNames[index]);
            } else {
                console.log("最近站点:" + index);
            }*/


        },
        error: function () {
            console.log("Connect Failed");
        }
    });
}

getCarLocation();
//通话代码
window.setInterval(function () {
    getCarLocation();
}, 6000);

function getRootPath_web() {
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPaht = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPaht + projectName);
}